package designpatterns.singleton;

/**
 * 单例模式（懒汉模式）
 */
public class LayzSingleton {

    private static volatile LayzSingleton instance = null; // 保证instance在所有线程同步

    // private 避免类在外部被实例化
    private LayzSingleton() {}

    public static synchronized LayzSingleton getInstance() {
        // getInstance 方法前加同步
        if(instance == null) {
            instance = new LayzSingleton();
        }
        return instance;
    }
}
